local guajie = fk.CreateSkill {
  name = "xiaobai__guajie"
}
Fk:loadTranslationTable {
  ["xiaobai__guajie"] = "卦解",
  [":xiaobai__guajie"] = "游戏开始时，你获得两个“辟易” （上限为8）；当你获得超出上限的“辟易”时，替换原有之一。",
  ["#xiaobai__guajie"] = "%arg（%arg2）",

  ["$xiaobai__guajie1"] = "阴阳相合，可释大道。",
  ["$xiaobai__guajie2"] = "天地相生，以化万物。"
}
guajie:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(guajie.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    for i = 1, 30, 1 do
      if not player:hasSkill("xiaobai__piyi" .. i) then
        table.insertIfNeed(skills, "xiaobai__piyi" .. i)
        room:handleAddLoseSkills(player, "xiaobai__piyi" .. i)
      end
      if #skills > 1 then break end
    end
  end,
})
guajie:addEffect(fk.EventAcquireSkill, {
  can_trigger = function(self, event, target, player, data)
    if data and data.who == player and player:hasSkill(guajie.name) then
      local skills = {}
      local names = {}
      for i = 1, 30, 1 do
        local name = "xiaobai__piyi" .. i
        if player:hasSkill(name) and data.skill.name ~= name then
          table.insertIfNeed(skills, name)
          table.insert(names, player:getMark(name) ~= 0 and player:getMark(name) or "nullification")
        end
      end
      event:setCostData(self, { skills = skills, names = names })
      return data.skill.name:startsWith("xiaobai__piyi") and #skills > 7
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {}
    local skills = event:getCostData(self).skills
    local names = event:getCostData(self).names
    for index, value in ipairs(skills) do
      table.insert(choices, "#xiaobai__guajie:::" .. value .. ":" .. names[index])
    end
    local skill = player.room:askToChoice(player, {
      skill_name = guajie.name,
      choices = choices,
      prompt = "卦解：选择一个“辟易”删除",
    })
    event:setCostData(self, { choice = skill })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skill = event:getCostData(self).choice
    local startPos, endPos = string.find(skill, ":::")
    if startPos then
      skill = string.sub(skill, endPos + 1)
    end
    startPos, endPos = string.find(skill, ":")
    if startPos then
      skill = string.sub(skill, 1, endPos - 1)
    end
    room:handleAddLoseSkills(player, "-" .. skill, nil, false)
  end,
})
return guajie
